﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Rendering;

namespace CGF
{
    public class PlayerBuilder
    {

        [MenuItem("CGFramework/打包 apk/测试包/仅打包", priority = -1000)]
        private static void _BuildApkToTestDir1()
        {
            BuildPack(false, false, false);
        }

        [MenuItem("CGFramework/打包 apk/测试包/自启动", priority = -1000)]
        private static void _BuildApkToTestDir2()
        {
            BuildPack(false, true, false);
        }

        [MenuItem("CGFramework/打包 apk/正式包/仅打包", priority = -1000)]
        private static void _BuildAllABToFormalDir1()
        {
            BuildPack(true, false, false);
        }

        [MenuItem("CGFramework/打包 apk/正式包/自启动", priority = -1000)]
        private static void _BuildAllABToFormalDir2()
        {
            BuildPack(true, true, false);
        }

        [MenuItem("CGFramework/打包 aab", priority = -1000)]
        private static void _BuildAABToToFormalDir()
        {
            BuildPack(true, false, true);
        }

        /// <summary>
        /// 打包Apk
        /// </summary>
        public static void BuildPack(bool isFormalPack, bool autoRun, bool buildAppBundle)
        {
            if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
            {
                Debug.LogError("该菜单仅Android包可用");
                return;
            }
            PlayerSettings.productName = isFormalPack ? CGFConfig.Instance.ProductName : CGFConfig.Instance.ProductNameTest;
            PlayerSettings.SetApplicationIdentifier(NamedBuildTarget.Android, isFormalPack ? CGFConfig.Instance.PackName : CGFConfig.Instance.PackName + ".Test");
            EditorUserBuildSettings.buildAppBundle = buildAppBundle;

            if (!isFormalPack)
            {
                CGFConfig.Instance.packs[CGFConfig.Instance.currPackIndex].currServerType = EResServerType.TestServer;
            }
            else
            {
                CGFConfig.Instance.packs[CGFConfig.Instance.currPackIndex].currServerType = EResServerType.FormalServer;
            }
            EditorUtility.SetDirty(CGFConfig.Instance);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            string fileName = $"{CGFConfig.Instance.FileName}_v{PlayerSettings.bundleVersion}";
            if (!isFormalPack) fileName += "_Test";
            string suffix = CGFConfig.Instance.isAppBundle ? "aab" : "apk";
            fileName += $".{suffix}";
            string path = $"{CGFConfig.Instance.ResExportDir}/{suffix}/{fileName}";

            BuildTarget platform = EditorUserBuildSettings.activeBuildTarget;
            BuildOptions options = autoRun && !buildAppBundle ? BuildOptions.AutoRunPlayer : BuildOptions.None;
            BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, path, platform, options);

            //删除BackUpThisFolder_ButDontShipItWithYourGame目录
            string[] directories = Directory.GetDirectories($"{CGFConfig.Instance.ResExportDir}/{suffix}", "*_DoNotShip", SearchOption.AllDirectories);
            foreach (string directory in directories)
            {
                Directory.Delete(directory, true);
            }

            //打开目录
            Application.OpenURL($"{CGFConfig.Instance.ResExportDir}/{suffix}");
        }
    }
}
